import member from "@src/store/member";
import base from "@src/store/base";
import router from "@src/router";
import {queryOfflineMember} from "@src/api/member";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: member,
    fields: ['isMember', 'memberName', 'phone', 'wxName', 'wxHeadImg'],
    actions: ['getBaseInfo'],
  }, {
    store: base,
    fields: ['themeColor', 'themeTitleColor', 'businessName']
  }],
  data: {},
  methods: {
    onLogin() {
      if (this.data.isMember) {
        wx.showModal({
          title: "提示",
          content: "您已经是会员了，点击将跳往首页",
          showCancel: false,
          success(res) {
            if (res.confirm) {
              router.push({name: "index"})
            }
          }
        });
        return;
      }
    },
    getPhoneNumber(e) {
      if (!e.detail.iv) {
        wx.showToast({title: "手机号获取失败，请重试", icon: "none", duration: 1500});
        return;
      }
      let data = {
        ivStr: e.detail.iv,
        encryptedData: e.detail.encryptedData,
      }
      queryOfflineMember(data).then(res => {
        if (res.data.offlineMember) {
          router.push({
            name: "member/bindOldMemberConfirm",
            success: (routerRes => {
              routerRes.eventChannel.emit('oldMemberData', {
                'ivStr': data.ivStr,
                'encryptedData': data.encryptedData,
                'oldMemberInfo': res.data
              })
            })
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "您不是老会员",
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                router.push("index");
              }
            }
          });
        }
      })
    },
  }
})
